# 导入数据库模块
import pymysql
# 导入Flask框架，这个框架可以快捷地实现了一个WSGI应用
from flask import Flask, flash
# 默认情况下，flask在程序文件夹中的templates子文件夹中寻找模块
from flask import render_template
# 导入前台请求的request模块
from flask import request, Blueprint, session
import traceback

shen_ZPXX = Blueprint('shen_zhaopinxinxi', __name__)

def sql_conn():
    conn = pymysql.connect(
        host='139.217.107.173',
        port=3306,
        user='root',
        password='123456',
        database='project_training',
        charset='utf8'
    )
    cur = conn.cursor()
    # 返回conn和cursor
    return conn, cur

# 关闭连接
def sql_close(conn, cur):
    conn.close()
    cur.close()

@shen_ZPXX.route('/shen_zhaopinxinxi', methods=['GET', 'POST'])
def shen_zhaopinxinxi():
    u = session.get('user')
    return render_template('zhaopinxinxi.html', u=u)

@shen_ZPXX.route('/shen_zhaopinxinxi2', methods=['GET', 'POST'])
def shen_zhaopinxinxi2():
    u = session.get('user')
    return render_template('zhaopinxinxi2.html', u=u)

@shen_ZPXX.route('/shen_search', methods=['GET', 'POST'])
def shen_search():
    conn, cur = sql_conn()
    sql = "SELECT * FROM zhaopin"
    print(sql)
    cur.execute(sql)
    u = cur.fetchall()
    sql_close(conn,cur)
    lis = []
    for i in u:
        lis2 = []
        for i2 in i:
            if i2 is None:
                lis2.append('NULL')
            else:
                lis2.append(i2)
        lis.append(lis2)
    data = ''
    for i in lis:
        data = data + '{' \
               + r'"id":' + r'"' + i[0] + r'",' \
               + r'"zhaopingangwei":' + r'"' + i[1] + r'",' \
               + r'"zhaopinrenshu":' + r'"' + i[2] + r'",' \
               + r'"email":' + r'"' + i[4] + r'",' \
               + r'"beizhu":' + r'"' + i[3] + r'"' \
               + "},"
    data = data[:-1]
    str = r'{"code":0,"msg":"","count":100,"data":[' + data + r']}'
    print(str)
    return str